﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using System.Net;

namespace CentralMonitoring
{
    /// <summary>
    /// 用户认证
    /// </summary>
    public partial class ServerEdit : Form
    {       
        public ServerEdit()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            IPAddress ip;
            int port;
            if (!IPAddress.TryParse(textBoxOld.Text,out ip))
            {
                MessageBox.Show("请输入正确IP地址！","提示");
                return;
            }
            if (!int.TryParse(textBoxNew.Text,out port))
            {
                MessageBox.Show("请输入正确端口号！", "提示");
                return;
            }
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            config.AppSettings.Settings["serverip"].Value =textBoxOld.Text;
            config.AppSettings.Settings["serverport"].Value = textBoxNew.Text;
            config.AppSettings.Settings["clientId"].Value = textBoxUUID.Text;
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
            MessageBox.Show("操作成功,请重新启动软件生效！", "提示");
            this.DialogResult = DialogResult.OK;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void ServerEdit_Load(object sender, EventArgs e)
        {
            textBoxOld.Text = ConfigurationManager.AppSettings["serverip"].ToString();
            textBoxNew.Text = ConfigurationManager.AppSettings["serverport"].ToString();
            textBoxUUID.Text = ConfigurationManager.AppSettings["clientId"].ToString();
        }
    }
}
